﻿using Chart3D;
using PGMHelper;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WSAP
{
    public static class WSlabBuilderNetExtension
    {
        /// <summary>
        /// 绘图
        /// </summary>
        /// <param name="model"></param>
        /// <param name="param"></param>
        public static void ToGraphics(this WSlabBuilderNet slab, SlabChartParam param)
        {
            //实例化窗体
            var chartForm = new ModelVisible(slab, param);
            //窗体显示
            chartForm.ShowDialog();
        }

        /// <summary>
        /// 生成板模型
        /// </summary>
        /// <param name="model"></param>
        /// <param name="control"></param>
        /// <returns></returns>
        public static Chart3DMain GetSlabChart3D(this WSlabBuilderNet slab, Control control, SlabChartParam param)
        {
            //底色修改
            control.BackColor = Color.White;
            //初始化
            var chart3D = new Chart3DMain(control, isColorBar: true, type: AxisBoundType.XYEqual, isInterp: true, interpNumber: 20);
            //设定透明度
            chart3D.DrawChart.SetColorMap(150);
            //遍历单元
            slab.GetElements().ForEach(element =>
            {
                //基本模型
                var data = element.GetSlabData(slab, param);
                if (data != null) chart3D.Add(data);
            });
            //获得最小最大值
            var valueBound = slab.GetValueBound(param);
            //最大值
            var descp = string.Format("Obj：{0}\nMin：{1}\nMax：{2}", 
                param.ChartEnum, valueBound.Item1, valueBound.Item2);
            //非模态窗体
            MessageBoxExtern.Asterisk(descp, isDialog: false);
            //返回绘图数据
            return chart3D;
        }

        /// <summary>
        /// 获得目标点位移
        /// </summary>
        /// <param name="slab"></param>
        /// <param name="node"></param>
        /// <returns></returns>
        public static double GetValue(this WSlabBuilderNet slab, WNodeNet node, SlabChartParam param)
        {
            switch (param.ChartEnum)
            {
                case SlabChartEnum.Deflection:
                    var defection = slab.GetDeflection(node.GetX(), node.GetY());         
                    return Math.Abs(defection) < 1E-4? 0: defection;
                default:
                    var m = slab.GetStress(node.GetX(), node.GetY(), (int)param.ChartEnum, param.Theta);
                    return Math.Abs(m) < 5E2 ? 0 : m;
            }
        }
        /// <summary>
        /// 获得值
        /// </summary>
        /// <param name="slab"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static Tuple<double, double> GetValueBound(this WSlabBuilderNet slab, SlabChartParam param)
        {
            //响应列表
            var valueList = new List<double>();
            //遍历单元
            slab.GetElements().ForEach(element => element.GetSlabValues(slab, param, valueList));
            //获得最小最大值
            var minValue = valueList.Min();
            var maxValue = valueList.Max();
            //返回最小最大值
            var TminValue = minValue < 0 ? minValue : 0;
            var TmaxValue = maxValue > 0 ? maxValue : 0;
            return Tuple.Create<double, double>(TminValue, TmaxValue);
        }
    }
}
